using MediatR;
using Microsoft.AspNetCore.Mvc;
using Traceability.API.APPLications.Commands.ImmunityInfo;
using Traceability.API.Dtos;
using Traceability.ErrorCode;

namespace Traceability.API.Controllers
{
    /// <summary>
    /// 免疫信息控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class ImmunityInfoController : ControllerBase
    {
        private readonly IMediator _mediator;

        public ImmunityInfoController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 添加免疫信息
        /// </summary>
        /// <param name="command">添加免疫信息命令</param>
        /// <returns>添加结果</returns>
        [HttpPost]
        public async Task<APIResult<ImmunityInfoDto>> Add([FromBody] AddImmunityInfoCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 获取免疫信息列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="searchKeyword">搜索关键词</param>
        /// <returns>免疫信息列表</returns>
        [HttpGet]
        public async Task<APIResult<PagedResultDto<ImmunityInfoDto>>> GetList(
            [FromQuery] int pageIndex = 1,
            [FromQuery] int pageSize = 10,
            [FromQuery] string? searchKeyword = null)
        {
            var query = new GetAllImmunityInfoQuery
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                SearchKeyword = searchKeyword
            };

            return await _mediator.Send(query);
        }

        /// <summary>
        /// 更新免疫信息
        /// </summary>
        /// <param name="command">更新免疫信息命令</param>
        /// <returns>更新结果</returns>
        [HttpPut]
        public async Task<APIResult<ImmunityInfoDto>> Update([FromBody] UpdateImmunityInfoCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 删除免疫信息
        /// </summary>
        /// <param name="id">免疫信息ID</param>
        /// <returns>删除结果</returns>
        [HttpDelete("{id}")]
        public async Task<APIResult<bool>> Delete(int id)
        {
            var command = new DeleteImmunityInfoCommand { ID = id };
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 根据生产批次查询免疫信息
        /// </summary>
        /// <param name="productionBatch">生产批次</param>
        /// <returns>免疫信息列表</returns>
        [HttpGet("by-batch")]
        public async Task<APIResult<List<ImmunityInfoDto>>> GetByBatch([FromQuery] string productionBatch)
        {
            var query = new GetImmunityInfoByBatchQuery { ProductionBatch = productionBatch };
            return await _mediator.Send(query);
        }
    }
} 